package cn.rayland.rayland3d.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import cn.rayland.app.yandktest.Machine;
import cn.rayland.library.utils.MachineManager;
import cn.rayland.rayland3d.R;

/**
 * Created by gw on 2016/3/28.
 */
public class StateActivity extends AppCompatActivity {
    private Handler handler = new Handler();
    private Runnable runnable;
    private Button cancelPrint;
    private TextView printState;
    private MachineManager machineManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_state);
        initView();
        startGetPrintState();
        machineManager = MachineManager.getInstance(this);
    }

    private void initView() {
        printState = (TextView) findViewById(R.id.printer_state);
        cancelPrint = (Button) findViewById(R.id.stop_print);
        cancelPrint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                machineManager.cancel(true);
                if (EasySharePreference.getPrefInstance(getApplicationContext()).getBoolean("ifpush", false)){
                    WebSocketPusher.stopPush();
                }
                finish();
            }
        });
    }

    //定时获取打印机状态
    private void startGetPrintState() {
        runnable = new Runnable(){
            @Override
            public void run() {
                try {
                    printState.setText(machineManager.getMachineState().toString());

                }catch (Exception e){}
                handler.postDelayed(this, 1000);
            }
        };
        handler.postDelayed(runnable, 1000);
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacks(runnable);
        super.onDestroy();

    }
}
